package com.genersoft.iot.vmp.baidu;

import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

import javax.imageio.ImageIO;

public class Base64Util{

    /**
     * 字符串转图片
     * @param base64Str
     * @return
     */
    public static void baseToFile(String base64Str,String path){
        try {
            byte[] imageBytes = Base64.getDecoder().decode(base64Str);
            OutputStream stream = new FileOutputStream(path); // 替换为您想要保存的图片文件名
            stream.write(imageBytes);
            stream.close();
            System.out.println("图片保存成功。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片转BASE64
     * @param imagePath
     * @return
     */
    public static String fileToBase64(String imagePath) {
        try {
            File file = new File(imagePath);
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            fis.read(bytes);
            fis.close();
            return Base64.getEncoder().encodeToString(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }



}
